﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;


namespace SPEventReceiverListing.Layouts.SPEventReceiversListing
{
    public partial class ViewAllEventReceivers : LayoutsPageBase
    {
        #region Declarations
        private DataTable dataTable = new DataTable("AllEvents");
        SPList spList = null;
        private readonly string _ID = "Id";
        private readonly string TYPE = "Type";
        private readonly string NAME = "Name";
        private readonly string CLASS = "Class";
        private readonly string ASSEMBLY = "Assembly";
        private readonly string SEQUENCENO = "SequenceNumber";
        private readonly string SYNCHRONIZATION = "Synchronization";
        private readonly string DATA = "Data";
        #endregion

        #region Methods
        protected override void OnInit(EventArgs e)
        {
            try
            {
                this.lblPageTitle.Text = GetResourceValue("PageTitle");
               
            }
            catch (Exception ex)
            {
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("SPEventReceiversListing", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
            }

             
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            { 
                spList = SPContext.Current.Web.Lists[new Guid(GetQueryStringVal("ListId"))] as SPList;
                if (null != spList && spList.DoesUserHavePermissions(SPBasePermissions.ManageLists))
                {
                    this.lblPageTitleInTitleArea.Text = string.Format(GetResourceValue("PageTitleInTitleArea"),spList.Title);
                    BuildTableSchema();
                    GetAllEventRegisteredReceivers();
                    BindToGridView();
                }
                else
                {
                    SPUtility.HandleAccessDenied(new Exception());
                }
            }
            catch (Exception ex)
            {
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("SPEventReceiversListing", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
            }



        }
        private void BindToGridView()
        {

            if (dataTable.Rows.Count > 0)
            {

                BoundField Id = new BoundField();
                Id.HeaderText = "ID";
                Id.DataField = _ID;
                this.grdView.Columns.Add(Id);

                BoundField type = new BoundField();
                type.HeaderText = "Type";
                type.DataField = TYPE;
                this.grdView.Columns.Add(type);

                BoundField name = new BoundField();
                name.HeaderText = "Name";
                name.DataField = NAME;
                this.grdView.Columns.Add(name);

                BoundField _class = new BoundField();
                _class.HeaderText = "Class";
                _class.DataField = CLASS;
                this.grdView.Columns.Add(_class);

                BoundField assembly = new BoundField();
                assembly.HeaderText = "Assembly";
                assembly.DataField = ASSEMBLY;
                this.grdView.Columns.Add(assembly);

                BoundField sequenceNo = new BoundField();
                sequenceNo.HeaderText = "Sequence No";
                sequenceNo.DataField = SEQUENCENO;
                this.grdView.Columns.Add(sequenceNo);

                BoundField synchronization = new BoundField();
                synchronization.HeaderText = "Synchronization";
                synchronization.DataField = SYNCHRONIZATION;
                this.grdView.Columns.Add(synchronization);

                BoundField data = new BoundField();
                data.HeaderText = "Data";
                data.DataField = DATA;
                this.grdView.Columns.Add(data);


                this.grdView.DataSource = this.dataTable.DefaultView;
                this.grdView.DataBind();
            }
        }
        private void BuildTableSchema()
        {
            dataTable.Columns.Add(_ID, typeof(string));
            dataTable.Columns.Add(TYPE, typeof(string));
            dataTable.Columns.Add(NAME, typeof(string));
            dataTable.Columns.Add(CLASS, typeof(string));
            dataTable.Columns.Add(ASSEMBLY, typeof(string));
            dataTable.Columns.Add(SEQUENCENO, typeof(string));
            dataTable.Columns.Add(SYNCHRONIZATION, typeof(string));
            dataTable.Columns.Add(DATA, typeof(string));

        }
        private void GetAllEventRegisteredReceivers()
        {

            if (null != spList)
            {
                SPEventReceiverDefinitionCollection regEvents = spList.EventReceivers;
                int count = regEvents.Count;
                if (null != regEvents && count > 0)
                {
                    this.lblCount.Text = string.Format(GetResourceValue("MsgTotalEvents"), spList.Title, count);
                    DataRow row = null;
                    foreach (SPEventReceiverDefinition eventDef in regEvents)
                    {
                        row = dataTable.NewRow();
                        row[TYPE] = eventDef.Type.ToString();
                        row[NAME] = eventDef.Name.ToString();
                        row[SEQUENCENO] = eventDef.SequenceNumber.ToString();
                        row[_ID] = eventDef.Id.ToString();
                        row[CLASS] = eventDef.Class.ToString();
                        row[ASSEMBLY] = eventDef.Assembly.ToString();
                        row[SYNCHRONIZATION] = eventDef.Synchronization.ToString();

                        if (!string.IsNullOrEmpty(eventDef.Data))
                        {
                            row[DATA] = eventDef.Data;

                        }
                        else
                        {
                            row[DATA] = "N/A";
                        }


                        this.dataTable.Rows.Add(row);
                    }
                }
                else
                {
                    this.lblCount.Text = string.Format(GetResourceValue("MsgNoEvent"), spList.Title);

                }
            }
        }
        #endregion

        #region Utility Methods
        private string GetQueryStringVal(string key)
        {
            try
            {
                return Request.QueryString[key];
            }
            catch
            {
                throw;
            }
        }
        private string GetResourceValue(string key)
        {
            try
            {
                return SPUtility.GetLocalizedString(string.Format("$Resources:{0}",key), "SPEventReceiversListing",
                                                            SPContext.Current.Web.Language);

            }
            catch
            {
                throw;
            }
        }
        #endregion
    }
}
